#define _CRT_SECURE_NO_WARNINGS 1
class Partition {
public:
    ListNode* partition(ListNode* pHead, int x) {
        struct ListNode* lesshead, * lesstail, * morehead, * moretail;
        lesshead = (struct ListNode*)malloc(sizeof(struct ListNode));
        morehead = (struct ListNode*)malloc(sizeof(struct ListNode));


        lesstail = lesshead;
        moretail = morehead;


        struct ListNode* cur = pHead;
        while (cur)
        {
            if (cur->val < x)
            {
                lesstail->next = cur;
                lesstail = lesstail->next;
            }
            else
            {
                moretail->next = cur;
                moretail = moretail->next;
            }
            cur = cur->next;
        }


        lesstail->next = morehead->next;
        moretail->next = NULL;


        pHead = lesshead->next;
        free(lesshead);
        free(morehead);
        return pHead;
    }
};